/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.admin.api.entity.SysUser;
import com.excesys.exsecs.admin.api.feign.RemoteUserService;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.om.constans.OmCommonConstants;
import com.excesys.exsecs.om.dto.*;
import com.excesys.exsecs.om.entity.*;
import com.excesys.exsecs.om.mapper.*;
import com.excesys.exsecs.om.service.OmICheckSheetService;
import com.excesys.exsecs.om.service.OmICheckUserRelService;
import com.excesys.exsecs.om.service.OmInspectionResultService;
import com.excesys.exsecs.om.utils.DateUtils;
import com.excesys.exsecs.om.utils.OrderNoUtils;
import com.excesys.exsecs.om.vo.InspectionVo;
import com.excesys.exsecs.om.vo.PointVo;
import com.excesys.exsecs.om.vo.RouteVo;
import com.excesys.exsecs.om.vo.SheetVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 巡检单
 *
 * @author weiyj
 * @date 2023-03-01 14:10:58
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OmICheckSheetServiceImpl extends ServiceImpl<OmICheckSheetMapper, OmICheckSheet>
		implements OmICheckSheetService {

	private final SequenceMapper sequenceMapper;

	private final OrderNoUtils orderNoUtils;

	private final OmICheckRouteMapper omICheckRouteMapper;

	private final OmICheckPointMapper omICheckPointMapper;

	private final OmIPointAssetRelMapper omIPointAssetRelMapper;

	private final OmICheckUserRelMapper omICheckUserRelMapper;

	private final OmICheckUserRelService omICheckUserRelService;

	private final OmIRoutePointRelMapper omIRoutePointRelMapper;

	private final OmICheckSheetDetailMapper omICheckSheetDetailMapper;

	private final OmInspectionResultService omInspectionResultService;

	private final OmInspectionResultMapper omInspectionResultMapper;

	private final OmInspectionAreaMapper omInspectionAreaMapper;

	private final OmICheckPlanMapper omICheckPlanMapper;

	private final OmICheckSheetMapper omICheckSheetMapper;

	private final RemoteUserService userService;

	private final OmInspectionItemsMapper omInspectionItemsMapper;


	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveSheet(SheetDto sheetDto) {

		OmICheckSheet omICheckSheet = new OmICheckSheet();
		BeanUtil.copyProperties(sheetDto, omICheckSheet);
		int checkSeq = sequenceMapper.getSeqNextVal("checkseq");
		omICheckSheet.setCheckNo(orderNoUtils.getOrderNumber("Check", checkSeq));
		omICheckSheet.setSheetStatus(OmCommonConstants.SHEET_STATUS_1);

		baseMapper.insert(omICheckSheet);
		// 保存巡检计划巡检人信息
		Optional.ofNullable(sheetDto.getUserRelList()).ifPresent(userRelLists -> {
			userRelLists.stream().map(omICheckUserRel -> {
				omICheckUserRel.setCheckNo(omICheckSheet.getCheckNo());
				R<SysUser> userInfo = userService.getUserById(omICheckUserRel.getCheckUserId());
				omICheckUserRel.setCheckUserName(userInfo.getData().getUsername());
				omICheckUserRel.setCheckUserPhone(userInfo.getData().getPhone());
				return omICheckUserRel;
			}).forEach(omICheckUserRelService::saveOrUpdate);
		});

		return Boolean.TRUE;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean updateSheet(SheetDto sheetDto) {

		OmICheckSheet omICheckSheet = new OmICheckSheet();
		BeanUtil.copyProperties(sheetDto, omICheckSheet);
		// int checkSeq = sequenceMapper.getSeqNextVal("checkseq");
		// omICheckSheet.setCheckNo(orderNoUtils.getOrderNumber("Check",checkSeq));
		omICheckSheet.setSheetStatus(OmCommonConstants.SHEET_STATUS_1);

		baseMapper.updateById(omICheckSheet);
		omICheckUserRelMapper
				.delete(Wrappers.<OmICheckUserRel>lambdaQuery().eq(OmICheckUserRel::getCheckNo, sheetDto.getCheckNo()));
		// 保存巡检计划巡检人信息
		Optional.ofNullable(sheetDto.getUserRelList()).ifPresent(userRelLists -> {
			userRelLists.stream().map(omICheckUserRel -> {
				omICheckUserRel.setCheckNo(omICheckSheet.getCheckNo());
				R<SysUser> userInfo = userService.getUserById(omICheckUserRel.getCheckUserId());
				omICheckUserRel.setCheckUserName(userInfo.getData().getUsername());
				omICheckUserRel.setCheckUserPhone(userInfo.getData().getPhone());
				return omICheckUserRel;
			}).forEach(omICheckUserRelService::saveOrUpdate);
		});

		return Boolean.TRUE;
	}

	@Override
	public R<Boolean> operateOmICheckSheet(SheetDto sheetDto) {

		String sheetStatus = sheetDto.getSheetStatus();
		String returnMsg = "";
		Boolean isok = true;

		OmICheckSheet omICheckSheet = new OmICheckSheet();
		// 指派或者申领
		if (sheetStatus.equals("2")) {
			omICheckSheet.setSheetStatus(sheetStatus);
			omICheckSheet.setActualUserId(sheetDto.getCheckUserId());
			omICheckSheet.setStId(sheetDto.getStId());
			omICheckSheet.setCheckUserId(sheetDto.getCheckUserId());
			omICheckSheet.setCheckDeptId(sheetDto.getCheckDeptId());
			omICheckSheet.setCheckUserName(sheetDto.getCheckUserName());
			omICheckSheet.setCheckUserPhone(sheetDto.getCheckUserPhone());
			returnMsg = "巡检单已指派";
		}
		else if (sheetStatus.equals("3")) {
			//
			omICheckSheet.setStartTime(
					LocalDateTime.parse(DateUtil.now(), DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A)));
			omICheckSheet.setStId(sheetDto.getStId());
			omICheckSheet.setSheetStatus(sheetStatus);
			returnMsg = "巡检单已开始";

		}
		else if (sheetStatus.equals("4")) {
			//
			omICheckSheet.setEndTime(
					LocalDateTime.parse(DateUtil.now(), DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A)));
			omICheckSheet.setStId(sheetDto.getStId());
			omICheckSheet.setSheetStatus(sheetStatus);
			returnMsg = "巡检单已结束";

		}

		this.getBaseMapper().updateById(omICheckSheet);
		return R.ok(isok, returnMsg);
	}

	@Override
	public IPage<SheetVo> getSheetWithRelPage(Page page, OmICheckSheet omICheckSheet, String flag) {
		if ("1".equals(flag)) {
			return baseMapper.getSheetWithRelTodayPage(page, omICheckSheet);
		}
		else {
			return baseMapper.getSheetWithRelPage(page, omICheckSheet);
		}
	}

	@Override
	public SheetVo getSheetById(Serializable id) {
		SheetVo sheetVo = new SheetVo();
		OmICheckSheet omICheckSheet = baseMapper.selectById(id);
		BeanUtil.copyProperties(omICheckSheet, sheetVo);
		RouteVo routeVo = new RouteVo();
		OmICheckRoute omICheckRoute;
		omICheckRoute = omICheckRouteMapper.selectById(omICheckSheet.getRouteId());
		BeanUtil.copyProperties(omICheckRoute, routeVo);
		OmIRoutePointRel omIRoutePointRel = new OmIRoutePointRel();
		omIRoutePointRel.setRouteId(omICheckSheet.getRouteId());
		List<OmIRoutePointRel> pointRelList = omIRoutePointRelMapper.selectList(Wrappers.query(omIRoutePointRel));
		List<PointVo> pointList = new ArrayList<>();

		for (OmIRoutePointRel iRoutePointRel : pointRelList) {
			PointVo pointVo = new PointVo();

			OmICheckPoint omICheckPoint = omICheckPointMapper.selectById(iRoutePointRel.getPointId());
			BeanUtil.copyProperties(omICheckPoint, pointVo);
			OmIPointAssetRel omIPointAssetRel = new OmIPointAssetRel();
			omIPointAssetRel.setPointId(iRoutePointRel.getPointId());
			List<OmIPointAssetRel> assetList = omIPointAssetRelMapper.selectList(Wrappers.query(omIPointAssetRel));
			pointVo.setRelList(assetList);
			pointList.add(pointVo);
		}
		routeVo.setRelList(pointList);

		sheetVo.setRouteVo(routeVo);

		List<OmICheckUserRel> userRelList = omICheckUserRelMapper.listUserByCheckNo(omICheckSheet.getCheckNo());
		sheetVo.setUserRelList(userRelList);
		return sheetVo;
	}

	@Override
	public SheetVo getSheetDetailById(Serializable id) {
		SheetVo sheetVo = new SheetVo();
		OmICheckSheet omICheckSheet = baseMapper.selectById(id);
		BeanUtil.copyProperties(omICheckSheet, sheetVo);
		RouteVo routeVo = new RouteVo();
		OmICheckRoute omICheckRoute;
		omICheckRoute = omICheckRouteMapper.selectById(omICheckSheet.getRouteId());
		BeanUtil.copyProperties(omICheckRoute, routeVo);
		OmIRoutePointRel omIRoutePointRel = new OmIRoutePointRel();
		omIRoutePointRel.setRouteId(omICheckSheet.getRouteId());
		List<OmIRoutePointRel> pointRelList = omIRoutePointRelMapper.selectList(Wrappers.query(omIRoutePointRel));
		List<PointVo> pointList = new ArrayList<>();

		for (OmIRoutePointRel iRoutePointRel : pointRelList) {
			PointVo pointVo = new PointVo();

			OmICheckPoint omICheckPoint = omICheckPointMapper.selectById(iRoutePointRel.getPointId());
			BeanUtil.copyProperties(omICheckPoint, pointVo);
			OmIPointAssetRel omIPointAssetRel = new OmIPointAssetRel();
			omIPointAssetRel.setPointId(iRoutePointRel.getPointId());
			List<OmIPointAssetRel> assetList = omIPointAssetRelMapper.selectList(Wrappers.query(omIPointAssetRel));
			pointVo.setRelList(assetList);

			OmICheckSheetDetail omICheckSheetDetail = new OmICheckSheetDetail();
			omICheckSheetDetail.setPointId(iRoutePointRel.getPointId());
			omICheckSheetDetail.setCheckNo(omICheckSheet.getCheckNo());
			List<OmICheckSheetDetail> detailList = omICheckSheetDetailMapper
					.selectList(Wrappers.query(omICheckSheetDetail));
			pointVo.setDetailList(detailList);
			pointList.add(pointVo);
		}
		routeVo.setRelList(pointList);

		sheetVo.setRouteVo(routeVo);

		List<OmICheckUserRel> userRelList = omICheckUserRelMapper.listUserByCheckNo(omICheckSheet.getCheckNo());
		sheetVo.setUserRelList(userRelList);
		return sheetVo;
	}

	@Override
	public SheetVo getSheetDetailByNum(Serializable id) {
		SheetVo sheetVo = new SheetVo();
		OmICheckSheet omICheckSheet = baseMapper.selectById(id);
		BeanUtil.copyProperties(omICheckSheet, sheetVo);
		RouteVo routeVo = new RouteVo();
		OmICheckRoute omICheckRoute;
		omICheckRoute = omICheckRouteMapper.selectById(omICheckSheet.getRouteId());
		BeanUtil.copyProperties(omICheckRoute, routeVo);
		OmIRoutePointRel omIRoutePointRel = new OmIRoutePointRel();
		omIRoutePointRel.setRouteId(omICheckSheet.getRouteId());
		List<OmIRoutePointRel> pointRelList = omIRoutePointRelMapper.selectList(Wrappers.query(omIRoutePointRel));
		List<PointVo> pointList = new ArrayList<>();

		for (OmIRoutePointRel iRoutePointRel : pointRelList) {
			OmInspectionResult or = omInspectionResultService.getFlag(iRoutePointRel.getPointId().toString(),
					omICheckSheet.getCheckNo());
			// 无序且未打卡点不展示
			if (("0".equals(routeVo.getIsSort()) && ObjectUtil.isNotEmpty(or)) || ("1".equals(routeVo.getIsSort()))) {
				PointVo pointVo = new PointVo();
				OmICheckPoint omICheckPoint = omICheckPointMapper.selectById(iRoutePointRel.getPointId());
				List<OmInspectionResult> results = omInspectionResultMapper.getResultList(omICheckSheet.getCheckNo(),
						iRoutePointRel.getPointId().toString());
				BeanUtil.copyProperties(omICheckPoint, pointVo);
				if (results.size() == 0)
					pointVo.setFlag("0");
				else
					pointVo.setFlag("1");
				List<ValueDto> dtoList = new ArrayList<>();
				for (OmInspectionResult result : results) {
					ResultDto resultDto = new ResultDto();
					resultDto.setInspectionAreaClassification(result.getInspectionAreaClassification());
					resultDto.setInspectionSheetId(result.getInspectionSheetId());
					resultDto.setPointCode(result.getPointCode());
					ValueDto dto = omInspectionResultService.getList(resultDto);
					List<InspectionDto> dtos = dto.getInspectionDtolist();
					for (InspectionDto inspectionDto : dtos) {
						inspectionDto.setInspectionId(
								omInspectionItemsMapper.getItemsById(inspectionDto.getInspectionId()).getEvenInfo());
					}
					dtoList.add(dto);
				}
				pointVo.setDtoList(dtoList);
				OmIPointAssetRel omIPointAssetRel = new OmIPointAssetRel();
				omIPointAssetRel.setPointId(iRoutePointRel.getPointId());
				List<OmIPointAssetRel> assetList = omIPointAssetRelMapper.selectList(Wrappers.query(omIPointAssetRel));
				pointVo.setRelList(assetList);

				OmICheckSheetDetail omICheckSheetDetail = new OmICheckSheetDetail();
				omICheckSheetDetail.setPointId(iRoutePointRel.getPointId());
				omICheckSheetDetail.setCheckNo(omICheckSheet.getCheckNo());
				List<OmICheckSheetDetail> detailList = omICheckSheetDetailMapper
						.selectList(Wrappers.query(omICheckSheetDetail));
				pointVo.setDetailList(detailList);
				pointList.add(pointVo);
			}
		}
		routeVo.setRelList(pointList);
		sheetVo.setRouteVo(routeVo);
		List<OmICheckUserRel> userRelList = omICheckUserRelMapper.listUserByCheckNo(omICheckSheet.getCheckNo());
		sheetVo.setUserRelList(userRelList);
		return sheetVo;
	}

	@Override
	public Map<String, String> getSheetCountMap(Long userId) {

		return this.getBaseMapper().getSheetCountMap(userId);
	}

	/**
	 * 修改巡检点 - nfc状态 0否 1是
	 */
	@Override
	public int getUpdateNfcStatus(Map map) {
		Map<String, String> objectMap = baseMapper.getVerifyNfcStrData(map);
		log.info("输出NFC识别查询MAP", objectMap);
		if (objectMap != null) {
			String ipId = String.valueOf(objectMap.get("ip_id"));
			String checkNo = String.valueOf(map.get("checkNo"));
			OmInspectionResult or = new OmInspectionResult();
			or.setInspectionSheetId(checkNo);
			or.setPointCode(ipId);
			or.setInspectionId("");
			or.setNfcTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			or.setEquipmentStatus("0");
			or.setInspectionValue("1");
			List<OmInspectionResult> om = omInspectionResultMapper.getLists(checkNo, ipId, "");
			if (ObjectUtil.isNotEmpty(om)) {
				for (OmInspectionResult result : om) {
					result.setNfcTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
					omInspectionResultMapper.updateResult(result.getInspectionSheetId(), result.getPointCode(),
							result.getNfcTime());
				}
			}
			else {
				omInspectionResultMapper.insert(or);
			}
			OmICheckPoint omICheckPoint = new OmICheckPoint();
			omICheckPoint.setIpId(Integer.valueOf(ipId));
			omICheckPoint.setNfcStatus("1");
			return omICheckPointMapper.updateById(omICheckPoint);
		}
		return 0;
	}

	@Override
	public List<Map<String, Object>> getAllCheckStatus() {
		List<Map<String, Object>> mapList = new ArrayList<>();
		Map<String, Object> objectMap = new HashMap<>();
		// 巡检计划 --- 计划状态
		Map<String, String> planStatus = omICheckPlanMapper.getPlanStatus();
		objectMap.put("planStatus", planStatus);
		// 巡检任务 --- 任务状态
		Map<String, String> taskStatus = baseMapper.getSheetStatus();
		objectMap.put("taskStatus", taskStatus);

		// 巡检记录 --- 记录状态
		Map<String, String> inspectionStatus = new HashMap<>();
		// 异常总数
		List<OmInspectionResult> exceptionSize = omInspectionResultMapper.getInpsectionStatus();
		inspectionStatus.put("exceptionSize", exceptionSize.size() + "");
		// 正常总数
		List<OmInspectionResult> normalSize = omInspectionResultMapper.getNormalStatus();
		inspectionStatus.put("normalSize", normalSize.size() + "");
		objectMap.put("inspectionStatus", inspectionStatus);
		// 巡检对象 --- 对象类型
		Map<String, String> inspectionObject = new HashMap<>();
		List<OmInspectionArea> list = omInspectionAreaMapper.getAreasLists();
		int j = 0;
		for (OmInspectionArea omInspectionArea : list) {
			if(StringUtil.isNotEmpty(omInspectionArea.getAssociatedDevice())){
				String[] splitList = omInspectionArea.getAssociatedDevice().split(";");
				j = j + splitList.length;
			}
		}
		inspectionObject.put("regionSize", list.size() + "");
		inspectionObject.put("equipmentSize", j + "");
		objectMap.put("inspectionObject", inspectionObject);
		// 今日巡检人 --- 巡检方式
		Map<String, String> counsel = new HashMap<>();
		List<String> counselList = baseMapper.getOmICheckSheetList();
		counsel.put("counselSize", counselList.size() + "");

		// 现场扫码
		List<String> nfcList = omICheckRouteMapper.getNfcList();
		counsel.put("nfcList", nfcList.get(0));
		// 领取任务
		List<String> CheckSheetList = baseMapper.getOmICheckSheetCountList();
		counsel.put("CheckSheetList", CheckSheetList.get(0));
		objectMap.put("counsel", counsel);
		mapList.add(objectMap);
		return mapList;
	}

	@Override
	public List<Map<String, Object>> getCheckFinishingRate() {
		List<Map<String, Object>> mapList = new ArrayList<>();
		Map<String, Object> objectMap = new HashMap<>();
		// 全部
		Map<String, String> allMap = baseMapper.getCheckAll();
		objectMap.put("checkAll", allMap);
		// 本年
		Map<String, String> thisYearMap = baseMapper.getCheckThisYear();
		objectMap.put("checkThisYear", thisYearMap);
		// 本月
		Map<String, String> thisMonthMap = baseMapper.getCheckThisMonth();
		objectMap.put("checkThisMonth", thisMonthMap);
		// 本周
		Map<String, String> thisWeekMap = baseMapper.getCheckThisWeek();
		objectMap.put("checkThisWeek", thisWeekMap);
		// 今天
		Map<String, String> thisTodayMap = baseMapper.getCheckThisToday();
		objectMap.put("checkThisToday", thisTodayMap);
		mapList.add(objectMap);
		return mapList;
	}

	@Override
	public Map<String, List<InspectionVo>> getCheckReportStatistics(String flag) {
		Map<String, List<InspectionVo>> objectMap = new HashMap<>();
		List<InspectionVo> yearList = new ArrayList<>();
		List<InspectionInfoDto> inspectors = new ArrayList<>();
		Map<String, String> map = new HashMap<>();

		if ("year".equals(flag)) {
			inspectors = baseMapper.getInspector(flag);
			map.put("year", flag);
			log.info("本年巡检人：" + inspectors);
		}
		else if ("month".equals(flag)) {
			inspectors = baseMapper.getInspector(flag);
			map.put("month", flag);
			log.info("本月巡检人：" + inspectors);
		}
		else if ("week".equals(flag)) {
			inspectors = baseMapper.getInspector(flag);
			map.put("week", flag);
			log.info("本周巡检人：" + inspectors);
		}
		else if ("today".equals(flag)) {
			inspectors = baseMapper.getInspector(flag);
			map.put("today", flag);
			log.info("当天巡检人：" + inspectors);
		}

		for (InspectionInfoDto i : inspectors) {
			InspectionVo inspectionVo = new InspectionVo();
			inspectionVo.setInspector(i.getCheckUserName());
			inspectionVo.setAffiliatedUnit(i.getCheckUserId());
			yearList.add(inspectionVo);
		}

		for (InspectionVo inspectionVo : yearList) {

			R<SysUser> sysUser = userService.getUserById(Long.valueOf(inspectionVo.getAffiliatedUnit()));
			if (sysUser.getData() != null) {
				inspectionVo.setAffiliatedUnit(sysUser.getData().getDeptId()+"");
			}


			List<SheetVo> sheetList = baseMapper.getSheetNumInfo(map, inspectionVo.getInspector());
			inspectionVo.setInspectionNum(sheetList.size() + "");


			int i = 0;
			int j = 0;
			int k = 0;
			int n = 0;
			Long a1 = System.currentTimeMillis();
			for (SheetVo sheetVo : sheetList) {
				n = n + omIRoutePointRelMapper.getNumByInfo(map, sheetVo.getRouteId()).size();
				List<OmInspectionArea> areaList = omInspectionAreaMapper.getNum(map, sheetVo.getCheckNo());
				List<OmInspectionResult> resultNum = omInspectionResultMapper.getNormalResultNum(map,
						sheetVo.getCheckNo());
				j = j + resultNum.size();
				List<OmInspectionResult> results = omInspectionResultMapper.getAnomaliesResultNum(map,
						sheetVo.getCheckNo());
				k = k + results.size();

				for (OmInspectionArea omInspectionArea : areaList) {
					if (StringUtil.isNotEmpty(omInspectionArea.getAssociatedDevice())) {
						String[] s = omInspectionArea.getAssociatedDevice().split(";");
						i = s.length + i;
					}
				}
			}
			Long b1 = System.currentTimeMillis();
			System.out.println("总耗时："+(b1-a1)/1000+"s");
			inspectionVo.setInspectionAreasNum(
					n + "");
			inspectionVo.setInspectionDevicesNum(i + "");
			inspectionVo.setExecutedNum(
					omICheckPlanMapper.getToBeExecutedPlanList(map, inspectionVo.getInspector()).size() + "");
			inspectionVo.setTaskCompletedNum(
					omICheckSheetMapper.getTaskCompletedNum(map, inspectionVo.getInspector()).size() + "");
			inspectionVo.setTaskOverdueNum(
					omICheckSheetMapper.getTaskOverdueNums(map, inspectionVo.getInspector()).size() + "");
			inspectionVo.setNormalResultNum(j + "");
			inspectionVo.setAnomaliesResultNum(k + "");
		}

		objectMap.put("inspectionInfoList", yearList);
		System.out.println("objectMap:" + objectMap);
		return objectMap;
	}

	@Override
	public List<CountRepairDto> getCheckRepairAnalyze() {
		List<CountRepairDto> list = new ArrayList<>();
		CountRepairDto repairDto = new CountRepairDto();
		// 本年
		// 异常总数
		List<WarrantyMethodDto> yearExceptionList = omInspectionResultMapper.getInpsectionYearList();
		// 正常总数
		List<WarrantyMethodDto> yearNormalList = omInspectionResultMapper.getInpsectionNormalYearList();

		// 本月
		// 异常总数
		List<WarrantyMethodDto> monthExceptionList = omInspectionResultMapper.getMonthExceptionList();
		// 正常总数
		List<WarrantyMethodDto> monthNormalList = omInspectionResultMapper.getMonthNormalList();

		// 本周
		// 异常总数
		List<WarrantyMethodDto> theWeekExceptionList = omInspectionResultMapper.getTheWeekExceptionList();
		// 正常总数
		List<WarrantyMethodDto> theWeekNormalList = omInspectionResultMapper.getTheWeekNormalList();

		// 今天
		// 异常总数
		List<WarrantyMethodDto> todayExceptionList = omInspectionResultMapper.getExceptionByToday();
		// 正常总数
		List<WarrantyMethodDto> todayNormalList = omInspectionResultMapper.getNormalByToday();

		repairDto.setYearExceptionList(yearExceptionList);
		repairDto.setYearNormalList(yearNormalList);
		repairDto.setMonthExceptionList(monthExceptionList);
		repairDto.setMonthNormalList(monthNormalList);
		repairDto.setTheWeekNormalList(theWeekNormalList);
		repairDto.setTheWeekExceptionList(theWeekExceptionList);
		repairDto.setTodayExceptionList(todayExceptionList);
		repairDto.setTodayNormalList(todayNormalList);
		list.add(repairDto);
		return list;
	}

	@Override
	public List<InspectionVo> listBamSBuildings(String flag) {
		Map<String, List<InspectionVo>> map = getCheckReportStatistics(flag);
		List<InspectionVo> list = map.get("inspectionInfoList");
		return list.stream().map(buildings -> {
			InspectionVo pointExcelVo = new InspectionVo();
			BeanUtil.copyProperties(buildings, pointExcelVo);
			return pointExcelVo;
		}).collect(Collectors.toList());
	}

	@Override
	public List<InspectionVo> listBamSBuildingsByTime(CheckReportStatisticsDto dto) {
		Map<String, List<InspectionVo>> map = getCheckReportStatisticsByDate(dto);
		List<InspectionVo> list = map.get("inspectionInfoList");
		return list.stream().map(buildings -> {
			InspectionVo pointExcelVo = new InspectionVo();
			BeanUtil.copyProperties(buildings, pointExcelVo);
			return pointExcelVo;
		}).collect(Collectors.toList());
	}

	@Override
	public Map<String, List<InspectionVo>> getCheckReportStatisticsByDate(CheckReportStatisticsDto dto) {
		Map<String, List<InspectionVo>> objectMap = new HashMap<>();
		List<InspectionVo> yearList = new ArrayList<>();
		List<InspectionInfoDto> inspectors = new ArrayList<>();
		Map<String, String> map = new HashMap<>();
		inspectors = baseMapper.getInspectorByTime(dto.getStartTime(), dto.getEndTime());
		map.put("startTime", dto.getStartTime());
		map.put("endTime", dto.getEndTime());
		log.info("本年巡检人：" + inspectors);

		for (InspectionInfoDto i : inspectors) {
			InspectionVo inspectionVo = new InspectionVo();
			inspectionVo.setInspector(i.getCheckUserName());
			inspectionVo.setAffiliatedUnit(i.getCheckUserId());
			yearList.add(inspectionVo);
		}
		for (InspectionVo inspectionVo : yearList) {
			R<SysUser> sysUser = userService.getUserById(Long.valueOf(inspectionVo.getAffiliatedUnit()));
			if (sysUser.getData() != null) {
				inspectionVo.setAffiliatedUnit(sysUser.getData().getDeptId()+"");
			}
			List<SheetVo> sheetList = baseMapper.getSheetNumInfoByTime(dto.getStartTime(), dto.getEndTime(),
					inspectionVo.getInspector());
			inspectionVo.setInspectionNum(sheetList.size() + "");

			int i = 0;
			int j = 0;
			int k = 0;
			int n = 0;
			for (SheetVo sheetVo : sheetList) {
				n = n+ omIRoutePointRelMapper.getNumByTime(dto.getStartTime(), dto.getEndTime(), sheetVo.getRouteId()).size();
				List<OmInspectionArea> areaList = omInspectionAreaMapper.getNumByTime(dto.getStartTime(),
						dto.getEndTime(), sheetVo.getCheckNo());
				List<OmInspectionResult> resultNum = omInspectionResultMapper
						.getNormalResultNumByTime(dto.getStartTime(), dto.getEndTime(), sheetVo.getCheckNo());
				j = j + resultNum.size();
				List<OmInspectionResult> results = omInspectionResultMapper
						.getAnomaliesResultNumByTime(dto.getStartTime(), dto.getEndTime(), sheetVo.getCheckNo());
				k = k + results.size();

				for (OmInspectionArea omInspectionArea : areaList) {
					if (StringUtil.isNotEmpty(omInspectionArea.getAssociatedDevice())) {
						String[] s = omInspectionArea.getAssociatedDevice().split(";");
						i = s.length + i;
					}
				}
			}
			inspectionVo.setInspectionAreasNum(n + "");
			inspectionVo.setInspectionDevicesNum(i + "");
			inspectionVo.setExecutedNum(omICheckPlanMapper
					.getToBeExecutedPlanListByTime(dto.getStartTime(), dto.getEndTime(), inspectionVo.getInspector())
					.size() + "");
			inspectionVo.setTaskCompletedNum(omICheckSheetMapper
					.getTaskCompletedNumByTime(dto.getStartTime(), dto.getEndTime(), inspectionVo.getInspector()).size()
					+ "");
			inspectionVo.setTaskOverdueNum(omICheckSheetMapper
					.getTaskOverdueNumsByTime(dto.getStartTime(), dto.getEndTime(), inspectionVo.getInspector()).size()
					+ "");
			inspectionVo.setNormalResultNum(j + "");
			inspectionVo.setAnomaliesResultNum(k + "");
		}
		objectMap.put("inspectionInfoList", yearList);
		System.out.println("objectMap:" + objectMap);
		return objectMap;
	}

	@Override
	public Map<String, Integer> getSheetNum() {
		Map<String, Integer> map = new HashMap<>();
		// 今日已巡检
		List<OmICheckSheet> inspectedList = omICheckSheetMapper.getInspectedSheetByToday();
		map.put("inspectedList", inspectedList.size());
		// 今日未巡检
		List<OmICheckSheet> notInspectedList = omICheckSheetMapper.getNotInspectedSheetByToday();
		map.put("notInspectedList", notInspectedList.size());
		return map;
	}

}
